<template>
  <view :style="{ width: width, height: height }">
    <canvas :style="{ width: '100%', height: '100%' }" :id="chartId"></canvas>
  </view>
</template>

<script>
import * as echarts from 'echarts';

export default {
  props: {
    chartId: {
      type: String,
      required: true,
    },
    option: {
      type: Object,
      required: true,
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '300px',
    },
  },
  mounted() {
    this.myChart = echarts.init(document.getElementById(this.chartId));
    this.myChart.setOption(this.option);

    window.addEventListener('resize', () => {
      this.myChart.resize();
    });
  },
  watch: {
    option: {
      handler(newOption) {
        this.myChart.setOption(newOption);
      },
      deep: true,
    },
  },
};
</script>

<style scoped>
canvas {
  display: block;
}
</style>
